﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using IP.Core.IPCommon;
using IP.Core.IPException;
using IP.Core.IPData;
using IP.Core.IPUserService;

using BtdDS;
using BtdUS;
using BtdDS.CDBNames;

namespace BtdApp.ChucNang
{
    public partial class F500_DM_DU_AN_BOI_THUONG_DE : Form
    {
        public F500_DM_DU_AN_BOI_THUONG_DE()
        {
            InitializeComponent();
            format_controls();
        }

        #region Members
        DS_DM_DU_AN_BOI_THUONG DS_DM_DU_AN_BOI_THUONG = new DS_DM_DU_AN_BOI_THUONG();
        US_DM_DU_AN_BOI_THUONG m_us_dabt= new US_DM_DU_AN_BOI_THUONG();
        DataEntryFormMode m_e_form_mode = DataEntryFormMode.InsertDataState;

        #endregion

        #region Public Interface
        public void display_for_insert()
        {
            m_e_form_mode = DataEntryFormMode.InsertDataState;
            this.ShowDialog();
        }
        public void display_for_update(US_DM_DU_AN_BOI_THUONG ip_us_dabt)
        {
            m_e_form_mode = DataEntryFormMode.UpdateDataState;
            m_us_dabt = ip_us_dabt;
            this.ShowDialog();
        }
        #endregion

        #region Private Methods
        private void format_controls()
        {
            CControlFormat.setFormStyle(this);
            m_lbl_title.Font = new Font("Arial", 16);
            m_lbl_title.ForeColor = Color.DarkRed;
            m_lbl_title.TextAlign = ContentAlignment.MiddleCenter;
            this.KeyPreview = true;
            set_define_events();
        }

        private void set_inital_form_load()
        {
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.InsertDataState:
                    break;
                case DataEntryFormMode.SelectDataState:
                    break;
                case DataEntryFormMode.UpdateDataState:
                    us_object_2_form(m_us_dabt);
                    break;
                case DataEntryFormMode.ViewDataState:
                    break;
                default:
                    break;
            }
        }
        private void us_object_2_form(US_DM_DU_AN_BOI_THUONG ip_us_dm)
        {
            m_txt_ten_du_an.Text = ip_us_dm.strTEN_DU_AN;
            m_txt_dia_diem.Text = ip_us_dm.strDIA_DIEM;
            m_txt_hoi_dong_boi_thuong.Text = ip_us_dm.strHOI_DONG_BOI_THUONG;
            m_txt_ly_do.Text = ip_us_dm.strLY_DO;
            m_txt_ghi_chu.Text = ip_us_dm.strGHI_CHU;
        }

        private void form_2_us_object(US_DM_DU_AN_BOI_THUONG op_us_dm)
        {
            op_us_dm.dcID = op_us_dm.dcID;
            op_us_dm.strTEN_DU_AN = m_txt_ten_du_an.Text;
            op_us_dm.strDIA_DIEM = m_txt_dia_diem.Text;
            op_us_dm.strHOI_DONG_BOI_THUONG = m_txt_hoi_dong_boi_thuong.Text;
            op_us_dm.strLY_DO = m_txt_ly_do.Text;
            op_us_dm.strGHI_CHU = m_txt_ghi_chu.Text;
        }

        private bool check_validate_data_is_ok()
        {
            return true;
        }
        
        private void save_data()
        {
            if (check_validate_data_is_ok() == false) return;
            form_2_us_object(m_us_dabt);
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.InsertDataState:
                    m_us_dabt.Insert();
                    break;

                case DataEntryFormMode.SelectDataState:
                    break;

                case DataEntryFormMode.UpdateDataState:
                    m_us_dabt.Update();
                    break;

                case DataEntryFormMode.ViewDataState:
                    break;

                default:
                    break;
            }

            BaseMessages.MsgBox_Infor(10); //Dữ liệu đã cập nhật thành công
            this.Close();
        }
        #endregion

        #region Export Excel
        #endregion

        #region Events
        private void set_define_events()
        {            
            m_cmd_luu.Click += new EventHandler(m_cmd_luu_Click);
            m_cmd_exit.Click += new EventHandler(m_cmd_exit_Click);
            this.KeyDown += new KeyEventHandler(F500_DM_DU_AN_BOI_THUONG_DE_KeyDown);
            this.Load += new EventHandler(F500_DM_DU_AN_BOI_THUONG_DE_Load);
        }

        void F500_DM_DU_AN_BOI_THUONG_DE_Load(object sender, EventArgs e)
        {
            try
            {
                set_inital_form_load();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }

        void F500_DM_DU_AN_BOI_THUONG_DE_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.Escape:
                        this.Close();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }

        void m_cmd_luu_Click(object sender, EventArgs e)
        {
            try
            {
                save_data();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }        

        void m_cmd_exit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        #endregion        
    }
}
